package com.wiloon.java8x.thread;

/**
 * Created by wiloon on 3/26/17;
 */
class InterRunnable implements Runnable {
    private int x;
    private boolean flag = true;

    public synchronized void run() {
        if (flag) {
            try {
                wait();  //让线程处于等待状态
            } catch (InterruptedException e) {
                System.out.println(Thread.currentThread().getName() + "....exception..");
                flag = false;
            }
            System.out.println(Thread.currentThread().getName() + "....run.." + x++);
        }
    }

    public void changeFlag() {
        this.flag = false;
    }
}

public class ThreadMethod {
    public static void main(String args[]) {
        InterRunnable ir = new InterRunnable();
        Thread t1 = new Thread(ir);
        Thread t2 = new Thread(ir);
        Thread t3 = new Thread(ir);
        t1.setPriority(Thread.MAX_PRIORITY);   //设置最大优先级
        t2.setPriority(Thread.MAX_PRIORITY);
        t1.start();
        t2.start();
        t3.start();
        for (int i = 0; i < 100; i++) {
            if (i == 60) {
                t1.interrupt();
                t2.interrupt();
                t3.interrupt();
                break;   //当i=60时，就让程序结束
            }
            System.out.println(Thread.currentThread().getName() + "........" + i);
        }
        System.out.println("over");
    }
}